﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Admin
    {
        public int Add(KeRui.Model.Admin model, string Password)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@RoleIDs", SqlDbType.VarChar), new SqlParameter("@Password", SqlDbType.VarChar), new SqlParameter("@CreateIP", SqlDbType.VarChar), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@RoleFlags", SqlDbType.VarChar), new SqlParameter("@RoleNames", SqlDbType.VarChar), new SqlParameter("@PlusFlag", SqlDbType.VarChar), new SqlParameter("@RealName", SqlDbType.VarChar), new SqlParameter("@UserFlag", SqlDbType.VarChar) };
            parameters[0].Value = model.Username;
            parameters[1].Value = model.RoleIDs;
            parameters[2].Value = Password;
            parameters[3].Value = model.CreateIP;
            parameters[4].Value = model.State;
            parameters[5].Value = model.RoleFlags;
            parameters[6].Value = model.RoleNames;
            parameters[7].Value = model.PlusFlag;
            parameters[8].Value = model.RealName;
            parameters[9].Value = model.UserFlag;
            return DbHelperSQL.RunProcedure("Admin_ADD", parameters, out num);
        }

        public void Delete(int UserID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = UserID;
            DbHelperSQL.RunProcedure("Admin_Delete", parameters, out num);
        }

        public KeRui.Model.Admin GetModel(int UserID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = UserID;
            KeRui.Model.Admin admin = new KeRui.Model.Admin();
            DataSet set = DbHelperSQL.RunProcedure("Admin_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    admin.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                admin.Username = set.Tables[0].Rows[0]["Username"].ToString();
                admin.UserFlag = set.Tables[0].Rows[0]["UserFlag"].ToString();
                admin.RoleIDs = set.Tables[0].Rows[0]["RoleIDs"].ToString();
                admin.RoleNames = set.Tables[0].Rows[0]["RoleNames"].ToString();
                admin.RoleFlags = set.Tables[0].Rows[0]["RoleFlags"].ToString();
                admin.PlusFlag = set.Tables[0].Rows[0]["PlusFlag"].ToString();
                admin.RealName = set.Tables[0].Rows[0]["RealName"].ToString();
                admin.JobTitle = set.Tables[0].Rows[0]["JobTitle"].ToString();
                admin.JobDept = set.Tables[0].Rows[0]["JobDept"].ToString();
                admin.QQ = set.Tables[0].Rows[0]["QQ"].ToString();
                admin.MSN = set.Tables[0].Rows[0]["MSN"].ToString();
                admin.Email = set.Tables[0].Rows[0]["Email"].ToString();
                admin.Mobile = set.Tables[0].Rows[0]["Mobile"].ToString();
                admin.HomeTel = set.Tables[0].Rows[0]["HomeTel"].ToString();
                admin.OfficeTel = set.Tables[0].Rows[0]["OfficeTel"].ToString();
                if (set.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    admin.State = int.Parse(set.Tables[0].Rows[0]["State"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsFounder"].ToString() != "")
                {
                    admin.IsFounder = int.Parse(set.Tables[0].Rows[0]["IsFounder"].ToString());
                }
                admin.Guid = set.Tables[0].Rows[0]["Guid"].ToString();
                return admin;
            }
            return null;
        }

        public int Update(KeRui.Model.Admin model)
        {
            return 1;
        }

        public int Update(KeRui.Model.Admin model, string Password)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@UserFlag", SqlDbType.VarChar), new SqlParameter("@RoleIDs", SqlDbType.VarChar), new SqlParameter("@RoleNames", SqlDbType.NVarChar), new SqlParameter("@RoleFlags", SqlDbType.VarChar), new SqlParameter("@PlusFlag", SqlDbType.VarChar), new SqlParameter("@RealName", SqlDbType.VarChar), new SqlParameter("@Password", SqlDbType.VarChar), new SqlParameter("@State", SqlDbType.Int, 4) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.UserFlag;
            parameters[3].Value = model.RoleIDs;
            parameters[4].Value = model.RoleNames;
            parameters[5].Value = model.RoleFlags;
            parameters[6].Value = model.PlusFlag;
            parameters[7].Value = model.RealName;
            parameters[8].Value = Password;
            parameters[9].Value = model.State;
            return DbHelperSQL.RunProcedure("Admin_Update", parameters, out num);
        }

        public int updateAdminInfo(KeRui.Model.Admin model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@RealName", SqlDbType.NVarChar, 20), new SqlParameter("@JobTitle", SqlDbType.VarChar), new SqlParameter("@JobDept", SqlDbType.VarChar), new SqlParameter("@QQ", SqlDbType.NVarChar), new SqlParameter("@MSN", SqlDbType.VarChar), new SqlParameter("@Email", SqlDbType.VarChar), new SqlParameter("@Mobile", SqlDbType.VarChar), new SqlParameter("@HomeTel", SqlDbType.VarChar), new SqlParameter("@OfficeTel", SqlDbType.VarChar) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.RealName;
            parameters[2].Value = model.JobTitle;
            parameters[3].Value = model.JobDept;
            parameters[4].Value = model.QQ;
            parameters[5].Value = model.MSN;
            parameters[6].Value = model.Email;
            parameters[7].Value = model.Mobile;
            parameters[8].Value = model.HomeTel;
            parameters[9].Value = model.OfficeTel;
            return DbHelperSQL.RunProcedure("Admin_UpdateInfo", parameters, out num);
        }

        public int UpdateForPlusSysRoleFlag(KeRui.Model.Admin model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserFlag", SqlDbType.VarChar), new SqlParameter("@PlusFlag", SqlDbType.VarChar) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.UserFlag;
            parameters[2].Value = model.PlusFlag;
            return DbHelperSQL.RunProcedure("Admin_UpdatePlusSysRoleFlag", parameters, out num);
        }

        public int UpdatePlusFlagForSiteFlag(KeRui.Model.Admin model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserFlag", SqlDbType.VarChar), new SqlParameter("@PlusFlag", SqlDbType.VarChar) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.UserFlag;
            parameters[2].Value = model.PlusFlag;
            return DbHelperSQL.RunProcedure("Admin_UpdatePlusSysRoleFlag", parameters, out num);
        }
    }
}

